package com.xb.loan.encryptor.crypt;


import com.xb.loan.encryptor.classloader.CryptographClassLoader;

import java.lang.reflect.Method;

public class OssCryptManager {
	
	public static final String PARTNER_GEN_PRIKEY = "genPri";
	
	public static final String PARTNER_GEN_PUBKEY = "genPub";
	
	public static final String PARTNER_PUBKEY = "partnerPub";
	// 类加载器
	private static CryptographClassLoader loader = new CryptographClassLoader(Thread.currentThread().getContextClassLoader());
	
	// 添加合作商密钥
	public static String genPartnerKey(String partnerId){
		
		Method method = getMethod("genPartnerKey", String.class);
		
		String result = null;
		
		try {
			
			if(method != null){
				
				result = (String) method.invoke(null,partnerId);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	// 查询合作商密钥
	public static String queryPartnerKey(String keyType, String partnerId){
		
		Method method = getMethod("queryPartnerKey", String.class, String.class);
		
		String result = null;
		
		try {
			
			if(method != null){
				
				result = (String) method.invoke(null,keyType,partnerId);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return result;
	}
	
	// 添加合作商公钥
	public static void addPartnerPubKey(String partnerId, String partnerPubKey){
		
		Method method = getMethod("addPartnerPubKey", String.class, String.class);
		
		try {
			
			if(method != null){
				
				method.invoke(null,partnerId,partnerPubKey);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static Method getMethod(String name, Class<?>...parameterTypes){
		
		Method method = null;
		
		try {
			Class<?> clazz = Class.forName("com.palm.plat.framework.cryptography.util.OssCryptHelper", true, loader);
			
			method = clazz.getMethod(name, parameterTypes);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return method;
	}
	
	public static void main(String[] args) {
		
		System.out.println(genPartnerKey("008600"));
	}
	
}
